package com.cloudpolice.equipment.controller;

import com.cloudpolice.equipment.model.EquipmentParamDefaultModel;
import com.cloudpolice.equipment.model.EquipmentParamModel;
import com.cloudpolice.equipment.service.EquipmentParamService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lh.model.ResultVO;
import lh.units.ResultStruct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author ：梁昊，后端工程师：卢春龙，前端工程师：孙耀
 * @create 2020-04-20 19:58
 * @function
 * @editLog
 */
@RestController
@RequestMapping("/equipmentParamController")
@Api(value = "设备参数", description = "设备参数详情")
public class EquipmentParamController {
    @Autowired
    EquipmentParamService equipmentParamService;

    /**
     * 根据sn号删除设备参数，方法ID：DE2020042019520982919B3C06D583A
     *
     * @param sn 设备SN
     * @return 结果
     */
    @ApiOperation(value = "根据sn号删除设备参数,方法ID：DE2020042019520982919B3C06D583A", notes = "结果")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "sn", value = "设备SN", dataType = "String", required = true)
    })
    @PostMapping("/deleteEquipmentParamBySn")
    public ResultVO deleteEquipmentParamBySn(@RequestParam(value = "sn") String sn) {
        sn = sn == null ? sn : sn.trim();

        int updateCount = equipmentParamService.deleteEquipmentParamBySn(sn);
        if (updateCount > 0)
            return ResultStruct.success(updateCount);
        else
            return ResultStruct.error("删除失败", ResultVO.class, int.class);
    }

    /**
     * 修改设备参数，方法ID：UP20200420200333258837B2714F35B
     *
     * @param id    主键，参数ID, Where字段
     * @param sn    设备SN, Where字段
     * @param param 参数
     * @return 结果
     */
    @ApiOperation(value = "修改设备参数,方法ID：UP20200420200333258837B2714F35B", notes = "结果")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "主键，参数ID", dataType = "String", required = true)
            , @ApiImplicitParam(name = "sn", value = "设备SN", dataType = "String", required = true)
            , @ApiImplicitParam(name = "param", value = "参数", dataType = "String", required = true)
    })
    @PostMapping("/updateEquipmentParam")
    public ResultVO updateEquipmentParam(@RequestParam(value = "id") String id
            , @RequestParam(value = "sn") String sn
            , @RequestParam(value = "param") String param
    ) {
        id = id == null ? id : id.trim();
        sn = sn == null ? sn : sn.trim();
        param = param == null ? param : param.trim();

        EquipmentParamModel equipmentParamModel = new EquipmentParamModel();
        equipmentParamModel.setId(id);
        equipmentParamModel.setSn(sn);
        equipmentParamModel.setParam(param);
        int updateCount = equipmentParamService.updateEquipmentParam(equipmentParamModel);
        if (updateCount > 0)
            return ResultStruct.success(updateCount);
        else
            return ResultStruct.error("修改失败", ResultVO.class, int.class);
    }

    /**
     * 通用查询设备参数，方法ID：SE20200420201948163965D6730E967
     *
     * @param id     表inf_EquipmentParam,字段名id:主键，参数ID
     * @param sn     表inf_EquipmentParam,字段名sn:设备SN
     * @param param  表inf_EquipmentParam,字段名param:参数
     * @param remark 表inf_EquipmentParam,字段名remark:备注
     * @return 明细
     */
    @ApiOperation(value = "通用查询设备参数,方法ID：SE20200420201948163965D6730E967", notes = "明细")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "主键，参数ID", dataType = "String"),
            @ApiImplicitParam(name = "sn", value = "设备SN", dataType = "String"),
            @ApiImplicitParam(name = "param", value = "参数", dataType = "String"),
            @ApiImplicitParam(name = "remark", value = "备注", dataType = "String")
    })
    @PostMapping("/selectEquipmentParamAll")
    public ResultVO selectEquipmentParamAll(@RequestParam(value = "id", required = false) String id
            , @RequestParam(value = "sn", required = false) String sn
            , @RequestParam(value = "param", required = false) String param
            , @RequestParam(value = "remark", required = false) String remark) {
        id = id == null ? id : id.trim();
        sn = sn == null ? sn : sn.trim();
        param = param == null ? param : param.trim();
        remark = remark == null ? remark : remark.trim();

        EquipmentParamModel equipmentParamModel = new EquipmentParamModel();
        equipmentParamModel.setId(id);
        equipmentParamModel.setSn(sn);
        equipmentParamModel.setParam(param);
        equipmentParamModel.setRemark(remark);

        return ResultStruct.success(equipmentParamService.selectEquipmentParamAll(equipmentParamModel));
    }

    /**
     * 插入设备默认参数，方法ID：IN2020042020284516378BD223D1EDD
     *
     * @param sn     设备SN
     * @return 结果
     */
    @ApiOperation(value = "插入设备默认参数(初始化参数),方法ID：IN2020042020284516378BD223D1EDD", notes = "结果")
    @ApiImplicitParams(
             @ApiImplicitParam(name = "sn", value = "设备SN", dataType = "String", required = true))
    @PostMapping("/insertEquipmentParamDefault")
    public ResultVO insertEquipmentParamDefault(@RequestParam(value = "sn") String sn) {

        //如果该sn之前有数据,则先删除
        int repetitionCount = equipmentParamService.insertEquipmentParamDefaultBeforeCheck(sn);
        if (repetitionCount > 0)
            equipmentParamService.deleteEquipmentParamBySn(sn);

        //查询默认表上的数据,遍历插入到设备参数表
        int resultCount = 0;
        EquipmentParamModel equipmentParamModel = new EquipmentParamModel();
        List<EquipmentParamDefaultModel> equipmentParamDefault = equipmentParamService.getEquipmentParamDefault();
        for (EquipmentParamDefaultModel equipmentParamDefaultModel : equipmentParamDefault) {
            equipmentParamModel.setId(equipmentParamDefaultModel.getId());
            equipmentParamModel.setSn(sn);
            equipmentParamModel.setParam(equipmentParamDefaultModel.getParam());
            equipmentParamModel.setRemark(equipmentParamDefaultModel.getRemark());
            resultCount = equipmentParamService.insertEquipmentParamDefault(equipmentParamModel);
            resultCount++;
        }
        if (resultCount > 0)
            return ResultStruct.success(resultCount);
        else
            return ResultStruct.error("增加失败", ResultVO.class, int.class);
    }


}



